import configparser
import os

from logger import log

config = configparser.ConfigParser()
config_path = os.path.join(os.path.dirname(__file__), 'config/config.ini')
config.read(config_path, encoding="utf-8")

# 源数据库
src_host = "localhost"
src_user = "root"
src_password = "root"
src_port = 3306
try:
    # 数据库
    src_host = config.get("src_mysql", "host")
    src_user = config.get("src_mysql", "user")
    src_password = config.get("src_mysql", "password")
    src_port = config.getint("src_mysql", "port")
except Exception as e:
    log.error(e)

# 目标数据库
tar_host = "localhost"
tar_user = "root"
tar_password = "root"
tar_port = 3306
try:
    # 数据库
    tar_host = config.get("tar_mysql", "host")
    tar_user = config.get("tar_mysql", "user")
    tar_password = config.get("tar_mysql", "password")
    tar_port = config.getint("tar_mysql", "port")
except Exception as e:
    log.error(e)


def get_latest_time(name):
    if name == "block_scan":
        return config.get("latest_time", "block_scan")


def set_latest_time(name, time):
    if name == "block_scan":
        config.set("latest_time", "block_scan", time)
        with open(config_path, 'w') as configfile:
            config.write(configfile)
